import {
	createRouter,
	createWebHistory,
	createWebHashHistory
} from "vue-router";

import {
	useLoading
} from 'vue-loading-overlay';

import {
	ref,
	h
} from 'vue';

const routes = [{
		path: '/',
		redirect: '/index'
	},
	{
		path: '/index',
		name: 'index',
		component: () => import('@/module/index/page/index.vue'),
		meta: {
			title: '首页'
		}
	},
	{
		path: '/search',
		name: 'search',
		component: () => import('@/components/index/search.vue'),
		meta: {
			title: '搜索结果'
		}
	},
	{
		path: '/modelEvaluating/:id',
		name: 'modelEvaluating',
		component: () => import('@/module/modelEvaluating/page/index.vue'),
		meta: {
			title: '模型评测'
		}
	},
	{
		path: '/orderDetail/:id',
		name: 'orderDetail',
		component: () => import('@/module/orderDetail/page/index.vue'),
		meta: {
			title: '订单详情',
			status: 'error'
		}
	},
	{
		path: '/success',
		name: 'success',
		component: () => import('@/module/orderDetail/page/success.vue'),
		meta: {
			title: '支付页面',
			status: 'unauth'
		}
	},
	{
		path: '/modelDetail',
		name: 'modelDetail',
		//redirect: '/modelDetail/goods',
		component: () => import('@/module/modelDetail/page/index.vue'),
		meta: {
			title: '模型详情'
		},
		children: [{
				path: 'goods/:id',
				name: 'goods',
				component: () => import('@/components/modelDetail/goods.vue'),
				meta: {
					title: '模型详情-单品信息',
					tab: '0',
					status: 'error'
				}
			},
			{
				path: 'series/:id',
				name: 'series',
				component: () => import('@/components/modelDetail/series.vue'),
				meta: {
					title: '模型详情-系列信息',
					tab: '1',
					status: 'error'
				}
			}, {
				path: 'firm/:id',
				name: 'firm',
				component: () => import('@/components/modelDetail/firm.vue'),
				meta: {
					title: '模型详情-厂商信息',
					tab: '2',
					status: 'error'
				}
			},
			{
				path: 'relateVideo/:id',
				name: 'relateVideo',
				component: () => import('@/components/modelDetail/relateVideo.vue'),
				meta: {
					title: '模型详情-视频信息',
					tab: '3',
					status: 'error'
				}
			}
		]
	},
	{
		path: '/user',
		name: 'user',
		redirect: '/user/follow',
		component: () => import('@/module/user/page/index.vue'),
		meta: {
			title: '个人中心'
		},
		children: [{
				path: 'follow',
				name: 'follow',
				component: () => import('@/components/user/follow.vue'),
				meta: {
					title: '个人中心-我的关注',
					tab: '1',
					status: 'unauth'
				}
			},
			{
				path: 'save',
				name: 'save',
				component: () => import('@/components/user/save.vue'),
				meta: {
					title: '个人中心-我的收藏',
					tab: '2',
					status: 'unauth'
				}
			}, {
				path: 'self',
				name: 'self',
				component: () => import('@/components/user/self.vue'),
				meta: {
					title: '个人中心-修改个人资料',
					tab: '3',
					status: 'unauth'
				}
			},
			{
				path: 'waitPay',
				name: 'waitPay',
				component: () => import('@/components/user/waitPayOrder.vue'),
				meta: {
					title: '个人中心-待支付订单',
					tab: '4',
					status: 'unauth'
				}
			}
		]
	},
	{
		path: '/error',
		name: 'error',
		component: () => import('@/module/exception/error.vue'),
		meta: {
			title: '页面错误',
			status: 'unauth'
		}
	}
]

const router = createRouter({
  //history: createWebHashHistory(),
	history: createWebHistory(),
	routes
})

router.beforeEach((to, from, next) => {
	const $loading = useLoading();
	const fullPage = ref(false);
	const loader = $loading.show({
		canCancel: true,
		onCancel: () => {},
		fullPage: false,
		backgroundColor: "#fff",
		opacity: 0.3,
		color: "#00aaff",
		loader: "dots"
	});

	if (to.matched.length == 0) { //未匹配到路由
		next({
			name: 'error'
		})
	}

	setTimeout(() => {
		if (to.meta.title) {
			document.title = to.meta.title;
		}
		loader.hide();
		next();
	}, 700);

	if(to.meta.status == 'error' || to.meta.status == 'unauth'){
		//判断是否登录，sessionStorage存在token
		let token = sessionStorage.getItem("token");
		if (token == null || token == undefined) {
			next({
				name: 'index',
				params: {
					status: to.meta.status
				}
			});
		}
	}
})

export default router
